/*
    20. Write a program in C to find the largest and smallest word in a string.

    Test Data :
    Input the string : It is a string with smallest and largest word.

    Expected Output :

    The largest word is 'smallest'
    and the smallest word is 'a' 
    in the string : 'It is a string with smallest and largest word.'.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_LEN 1024
#define MAX_STR 256

char *strs[MAX_STR];

char* getstr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p++ = ch;
    }
    *p = '\0';
    return str;
}

int getstrlen(char *str)
{
    int len = 0;
    char *p = str;
    while(*p != '\0'){
        len++;
        p++;
    }
    len++; // '\0'
    return len;
}

void getword(char *str)
{
    int i = 0;
    int len = 0;
    char *p;
    while(1){
        if(isalpha(*str)){
            len++;
        }else if(len){            
            p = malloc(len + 1);
            strncpy(p, str - len, len);
            *(p + len) = '\0';
            strs[i++] = p;
            len = 0;
        }
        if(*str == '\0') 
            break;
        str++;
    }
    strs[i] = NULL;
}

void printstr(char *s[])
{
    int i = 0;
    while(s[i] != NULL){
        printf("%s\n", s[i]);
        i++;
    }
}

char *findmaxstr(char *s[])
{
    int i = 0;
    char *p = s[0];
    while(s[i]){
        if(getstrlen(p) < getstrlen(s[i])){
            p = s[i];
        }
        i++;
    }
    return p;
}

char *findminstr(char *s[])
{
    int i = 0;
    char *p = s[0];
    while(s[i]){
        if(getstrlen(p) > getstrlen(s[i])){
            p = s[i];
        }
        i++;
    }
    return p;
}

int main(void)
{
    char *str = malloc(MAX_LEN);
    printf("Input the string : ");
    getstr(str);
    getword(str);
    //printstr(strs);
    printf("The largest word is '%s'\nand the smallest word is '%s'\nin the string : '%s'.", 
            findmaxstr(strs), findminstr(strs), str);

    return 0;
}